//
//  SRClassHomeworkStudentFinishViewController.m
//  StoryShow
//
//  Created by CyonLeuMBP on 2017/8/2.
//  Copyright © 2017年 qudiandu.cn. All rights reserved.
//

#import "SRClassHomeworkStudentFinishViewController.h"
#import "SRHomeworkFinishHeaderView.h"
#import "FZMCCacheManager.h"
#import "SRAudioPlayerManager.h"
#import "UIButton+FZGreenButton.h"

@interface SRClassHomeworkStudentFinishViewController ()

@property (strong, nonatomic) SRHomeworkFinishHeaderView *headerView;
@property (strong, nonatomic) UILabel *preTitleLabel;
@property (strong, nonatomic) UILabel *commentLabel;
@property (strong, nonatomic) UIView *recordContentView;
@property (strong, nonatomic) UIImageView *recordPlayImageView;
@property (strong, nonatomic) UILabel *recordTimeLabel;
@property (strong, nonatomic) NSString *mp3Path;
@property (strong, nonatomic) NSString *audioFileName;
@property (strong, nonatomic) UIActivityIndicatorView *activityView;


@end

@implementation SRClassHomeworkStudentFinishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"老师点评";
    
    self.headerView = [[[NSBundle mainBundle] loadNibNamed:@"SRHomeworkFinishHeaderView" owner:nil options:nil] firstObject];
    self.headerView.userType = SRSchoolClassUserTypeStudent;

    self.preTitleLabel = [[UILabel alloc] init];
    self.commentLabel = [[UILabel alloc] init  ];
    
    FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
    self.preTitleLabel.text = @"老师点评";
    self.preTitleLabel.textColor = [UIColor whiteColor];
    self.preTitleLabel.font = css.fontOfH5;
    self.commentLabel.textColor = [UIColor whiteColor];
    self.commentLabel.font = css.fontOfH7;
    
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.preTitleLabel];
    [self.view addSubview:self.commentLabel];
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.equalTo(self.view);
        make.height.mas_equalTo(125);
    }];
    [self.preTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view).offset(15);
        make.top.equalTo(self.headerView.mas_bottom).offset(15);
        make.height.mas_equalTo(25);
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view).offset(15);
        make.top.equalTo(self.preTitleLabel.mas_bottom).offset(10);
//        make.bottom.equalTo(self.view);
    }];
	[self.commentLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
	
	
	[self setupView];
	
	self.recordContentView.hidden = YES;

    [self.headerView updateCellInfo:self.detailModel];
    
    if (self.detailModel.comment && [self.detailModel.comment length] > 0) {
        self.commentLabel.text = self.detailModel.comment;
    } else {
        
        NSString *comment = nil;
        if ([self.detailModel.finishArray count] > 0) {
            SRWorkListItemModel *showModel = [self.detailModel.finishArray firstObject];
            if (showModel.comment && [showModel.comment length] > 0) {
                comment = showModel.comment;
            }
			
			if (showModel.comment_audio && [showModel.comment_audio length] > 0) {
				self.mp3Path = showModel.comment_audio;
				
				self.recordContentView.hidden = NO;
				NSInteger timeStamp = [showModel.comment_audio_timelen integerValue];
				if (timeStamp <= 60) {
					self.recordTimeLabel.text = [NSString stringWithFormat:@"%02ld秒", (long)timeStamp];
				} else {
					self.recordTimeLabel.text = [NSString stringWithFormat:@"%02ld分%02ld秒", timeStamp / 60, timeStamp % 60];
				}
				
			}
        }
        if (comment) {
            self.commentLabel.text = comment;
        } else {
            self.commentLabel.text = @"老师未点评，请等待老师点评。";
        }
		
		
    }

}

- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
	
	[[SRAudioPlayerManager sharedInstance] stopPlayRecordAudio];
	
}
- (void)setupView {
	
	FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
	
	self.recordContentView = [[UIView alloc] init];
	self.recordContentView.clipsToBounds = YES;
	self.recordContentView.layer.cornerRadius = 4;
	self.recordContentView.backgroundColor = [UIColor colorWithHex:0x10aa14];
	[self.view addSubview:self.recordContentView];
	
	WEAKSELF
	[self.recordContentView addTapGestureHandler:^(UITapGestureRecognizer *gestureRecognizer, UIView *view, CGPoint locationInView) {
			//            if (weakSelf.touchAudioBlock) {
			//                weakSelf.touchAudioBlock();
			//            }
		[weakSelf onUserRecordPlayButton:nil];
	}];
	
	self.recordPlayImageView = [[UIImageView alloc] init];
	self.recordTimeLabel = [[UILabel alloc] init];
	self.recordTimeLabel.textAlignment = NSTextAlignmentLeft;
	
	self.recordPlayImageView.image = [UIImage imageNamed:@"voice"];
	self.recordTimeLabel.textColor = [UIColor whiteColor];
	self.recordTimeLabel.font = css.fontOfH6;
	
	
	self.recordContentView.frame = CGRectMake(0, 0, 120, 44);
	[self.recordContentView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.leading.equalTo(self.commentLabel.mas_leading);
		make.top.equalTo(self.commentLabel.mas_bottom).offset(15);
		make.height.mas_equalTo(40);
		make.trailing.equalTo(self.view).offset(-86);
	}];
	
	[self.recordContentView addSubview:self.recordTimeLabel];
	[self.recordContentView addSubview:self.recordPlayImageView];
	
	[self.recordPlayImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.trailing.equalTo(self.recordContentView).offset(-20);
		make.centerY.equalTo(self.recordContentView);
		make.width.mas_equalTo(20);
		make.height.mas_equalTo(26);
	}];
	
	[self.recordTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.trailing.equalTo(self.recordPlayImageView.mas_leading).offset(-5);
		make.centerY.equalTo(self.recordPlayImageView);
		make.height.mas_equalTo(20);
	}];
	
	self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
	[self.recordContentView addSubview:self.activityView];
	
	[self.activityView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.leading.equalTo(self.recordContentView).offset(20);
		make.centerY.equalTo(self.recordContentView);
		make.height.mas_equalTo(40);
		make.width.mas_equalTo(50);
	}];
	SRAudioPlayerManager *audioPlayer = [SRAudioPlayerManager sharedInstance];
	[audioPlayer setAudioFilePath:nil];
	[audioPlayer setAudioFileName:nil];
	
	audioPlayer.audioPlayerCompletedBlock = ^(BOOL finish) {
//		[weakSelf.headerView setActivityAnimated:NO];
		[weakSelf.activityView stopAnimating];
	};
	
	audioPlayer.audioPlayerProgressBlock = ^(CGFloat progress) {
		if (progress > 0.2) {
				//                [weakSelf.activityView stopAnimating];
		}
		
	};
	
	audioPlayer.audioRecorderFailureBlock = ^(NSString *message) {
		[weakSelf showHUDErrorMessage:message];
	};
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self.headerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.equalTo(self.view);
        make.height.mas_equalTo(125);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Event

- (void)onUserRecordPlayButton:(id)sender {
	SRAudioPlayerManager *audioPlayer = [SRAudioPlayerManager sharedInstance];
	
//	if (self.isFinishedDetail) {
	NSString *url = self.mp3Path;
		
		if ([audioPlayer playingFileName] && [[audioPlayer playingFileName] isEqualToString:[url lastPathComponent]]) {
			if (audioPlayer.isPlaying) {
				[self.activityView stopAnimating];
				[audioPlayer pauseRecordAudio];
			} else {
				[self.activityView startAnimating];
				audioPlayer.audioFilePath = nil;
				[[SRAudioPlayerManager sharedInstance] setRecordAudioWithFileURL:url];
				[[SRAudioPlayerManager sharedInstance] startPlayRecordAudio];
				
			}
		}
		else {
			[self.activityView startAnimating];
			audioPlayer.audioFilePath = nil;
			
			[[SRAudioPlayerManager sharedInstance] setRecordAudioWithFileURL:url];
//			[[SRAudioPlayerManager sharedInstance] startPlayRecordAudio];
			
		}
//	} else {
//		NSString *url = self.mp3Path;
//
//		if ([audioPlayer playingFileName] && [[audioPlayer playingFileName] isEqualToString:[url lastPathComponent]]) {
//			if (audioPlayer.isPlaying) {
//				[self.activityView stopAnimating];
//				[audioPlayer pauseRecordAudio];
//			} else {
//				[self.activityView startAnimating];
//				[audioPlayer startPlayRecordAudio];
//			}
//		}
//		else {
//			[self.activityView startAnimating];
//			[[SRAudioPlayerManager sharedInstance] setRecordAudioWithFilePath:url];
//			[[SRAudioPlayerManager sharedInstance] startPlayRecordAudio];
//
//		}
//	}
}



@end
